package com.hunttown.mes.service;

import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.ScriptFilterUtils;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.WorkflowApplyLineDao;
import com.hunttown.mes.domain.WorkflowApplyLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2019-08-01 15:48:24
 */
@Service
public class WorkflowApplyLineService {

    private final static Logger logger = LoggerFactory.getLogger(WorkflowApplyLineService.class);

    private final WorkflowApplyLineDao workflowApplyLineDao;

    @Autowired
    public WorkflowApplyLineService(WorkflowApplyLineDao workflowApplyLineDao) {
        this.workflowApplyLineDao = workflowApplyLineDao;
    }

    /**
     * 新增
     *
     * @param workflowApplyLine 实体对象
     * @return 最新插入id
     */
    public WorkflowApplyLine insert(WorkflowApplyLine workflowApplyLine) {
        if (workflowApplyLine.getAdminId() == null)
            workflowApplyLine.setAdminId(0);
        if (workflowApplyLine.getStopFlag() == null)
            workflowApplyLine.setStopFlag(0);
        if (workflowApplyLine.getDeleteFlag() == null)
            workflowApplyLine.setDeleteFlag(0);

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(workflowApplyLine), WorkflowApplyLine.class))) {
            return workflowApplyLine;
        }
        return workflowApplyLineDao.insert(workflowApplyLine);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param workflowApplyLine 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(WorkflowApplyLine workflowApplyLine) {
        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(workflowApplyLine), WorkflowApplyLine.class))) {
            return false;
        }
        return workflowApplyLineDao.updateInfoById(workflowApplyLine);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (ScriptFilterUtils.xssFilterForQuery(query)) {
            return false;
        }
        return workflowApplyLineDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public WorkflowApplyLine selectById(Integer id) {
        return workflowApplyLineDao.selectById(id);
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public WorkflowApplyLine selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }

        List<WorkflowApplyLine> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<WorkflowApplyLine> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }

        return workflowApplyLineDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }

        return workflowApplyLineDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return workflowApplyLineDao.deleteById(id);
    }

    // 环节审批
    public Boolean changeState(Integer id, Integer state, String remark, Integer approver) {
        WorkflowApplyLine obj = new WorkflowApplyLine();
        obj.setIsCurrent(0);    //当前节点设置为0
        obj.setStatus(state);   //改变审批状态
        obj.setRemark(remark);  //审批意见
        obj.setRealApproverId(Integer.valueOf(approver.toString()));
        obj.setRealApproverTime(new Date());
        obj.setId(id);
        return updateInfoById(obj);
    }

    /**
     * 通过审批ID和环节顺序获取环节ID
     *
     * @param businessId 业务ID
     * @param applyId    审批ID
     * @param nodeSign   环节标记
     * @return
     */
    public Integer getLineIdByNodesign(Integer businessId, Integer applyId, Integer nodeSign) {
        Integer lineId = 0;

        Query query = Query.noPage();
        query.put("businessId", businessId);
        query.put("applyId", applyId);
        query.put("nodeSign", nodeSign);
        query.put("status", 0); //限制未审批，因为可能会有加签环节
        WorkflowApplyLine dtoLine = selectObjByQuery(query);
        if (dtoLine != null) {
            lineId = dtoLine.getId();
        }

        return lineId;
    }

    /**
     * 获取当前最新审批环节
     *
     * @param applyId 审批ID
     * @return
     */
    public Integer getCurrLineIdByApplyId(Integer applyId) {
        Integer lineId = 0;

        Query query = Query.noPage();
        query.put("applyId", applyId);
        query.put("isCurrent", 1);
        WorkflowApplyLine dtoLine = selectObjByQuery(query);
        if (dtoLine != null) {
            lineId = dtoLine.getId();
        }

        return lineId;
    }

    /**
     * 获取第一个审批环节ID
     *
     * @param applyId 审批ID
     * @return
     */
    public Integer getFirstLineIdByApplyId(Integer applyId) {
        Integer lineId = 0;

        Query query = Query.noPage();
        query.put("applyId", applyId);
        query.put("orderBy", "order by node_sort asc");
        WorkflowApplyLine dtoLine = selectObjByQuery(query);
        if (dtoLine != null) {
            lineId = dtoLine.getId();
        }

        return lineId;
    }

}